from rest_framework import serializers
from .models import KnowledgeNode, LearningPath, PathNode, StudyRecord, ReviewTask


class KnowledgeNodeSerializer(serializers.ModelSerializer):
    """知识节点序列化器"""
    prerequisites_info = serializers.SerializerMethodField()
    dependents_count = serializers.SerializerMethodField()

    class Meta:
        model = KnowledgeNode
        fields = '__all__'
        read_only_fields = ['user', 'created_at', 'updated_at']

    def get_prerequisites_info(self, obj):
        return [{'id': node.id, 'name': node.name} for node in obj.prerequisites.all()]

    def get_dependents_count(self, obj):
        return obj.dependents.count()

    def create(self, validated_data):
        validated_data['user'] = self.context['request'].user
        return super().create(validated_data)


class PathNodeSerializer(serializers.ModelSerializer):
    """路径节点序列化器"""
    node_info = KnowledgeNodeSerializer(source='node', read_only=True)

    class Meta:
        model = PathNode
        fields = '__all__'


class LearningPathSerializer(serializers.ModelSerializer):
    """学习路径序列化器"""
    target_node_name = serializers.CharField(source='target_node.name', read_only=True)
    node_count = serializers.SerializerMethodField()

    class Meta:
        model = LearningPath
        fields = '__all__'
        read_only_fields = ['user', 'progress', 'created_at', 'updated_at']

    def get_node_count(self, obj):
        return obj.nodes.count()

    def create(self, validated_data):
        validated_data['user'] = self.context['request'].user
        return super().create(validated_data)


class LearningPathDetailSerializer(LearningPathSerializer):
    """学习路径详情序列化器"""
    path_nodes = PathNodeSerializer(source='pathnode_set', many=True, read_only=True)

    class Meta:
        model = LearningPath
        fields = [
            'id',
            'title',
            'description',
            'status',
            'progress',
            'target_node',
            'target_node_name',
            'node_count',
            'estimated_hours',
            'path_nodes',
            'started_at',
            'completed_at',
            'created_at',
            'updated_at',
        ]
        read_only_fields = ['user', 'progress', 'created_at', 'updated_at', 'status']


class StudyRecordSerializer(serializers.ModelSerializer):
    """学习记录序列化器"""
    node_name = serializers.CharField(source='node.name', read_only=True)

    class Meta:
        model = StudyRecord
        fields = '__all__'
        read_only_fields = ['user', 'created_at']

    def create(self, validated_data):
        validated_data['user'] = self.context['request'].user
        return super().create(validated_data)


class ReviewTaskSerializer(serializers.ModelSerializer):
    """复习任务序列化器"""
    node_name = serializers.CharField(source='node.name', read_only=True)

    class Meta:
        model = ReviewTask
        fields = '__all__'
        read_only_fields = ['user', 'created_at']

    def create(self, validated_data):
        validated_data['user'] = self.context['request'].user
        return super().create(validated_data)
